/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.wdssii.gui.nbm.views;

import gov.nasa.worldwind.Model;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.awt.WorldWindowGLJPanel;
import gov.nasa.worldwind.layers.LayerList;
import gov.nasa.worldwind.render.DrawContext;
import java.awt.BorderLayout;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.wdssii.geom.Location;
import org.wdssii.gui.products.Product;
import org.wdssii.gui.views.EarthBallView;
import org.wdssii.gui.worldwind.LLHAreaLayer;
import org.wdssii.gui.worldwind.ProductLayer;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.wdssii.gui.nbm.views//Earth//EN",
autostore = false)
@TopComponent.Description(preferredID = "EarthTopComponent",
iconBase = "org/wdssii/gui/nbm/views/world.png",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "org.wdssii.gui.nbm.views.EarthTopComponent")
@ActionReference(path = "Menu/Window/WDSSII" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_EarthAction",
preferredID = "EarthTopComponent")
public final class EarthTopComponent extends TopComponent implements EarthBallView {

    private WorldWindowGLJPanel myWorld;
    /** The worldwind layer that holds our radar products */
    private ProductLayer myProducts;

    public EarthTopComponent() {
        initComponents();

        // Basic worldwind setup...
        Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
        myWorld = new WorldWindowGLJPanel();
        myWorld.setModel(m);
        jPanel1.setLayout(new BorderLayout());
        jPanel1.add(myWorld, BorderLayout.CENTER);

        // Either:
        // 1. current worldwind has a VBO bug
        // 2. Netbeans is using opengl somewhere and leaking opengl state (vbo)
        // 3. my nvidia driver is leaking..
        // 4.  something else.. lol
        // but we keep getting VBO exceptions...so turn it off in our worldwind for now...
        myWorld.getSceneController().getGLRuntimeCapabilities().setVertexBufferObjectEnabled(false);
        myProducts = new ProductLayer();
        myWorld.getModel().getLayers().add(myProducts);
        setName(NbBundle.getMessage(EarthTopComponent.class, "CTL_EarthTopComponent"));
        setToolTipText(NbBundle.getMessage(EarthTopComponent.class, "HINT_EarthTopComponent"));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 51, 51), 2));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
        );

        jToolBar1.setRollover(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void takeDialogSnapshot() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public LayerList getLayerList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setLayerEnabled(String name, boolean flag) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updateOnMinTime() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getProjection() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setProjection(String projection) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void loadProduct(Product aProduct) {
        if (myWorld != null) {
            myWorld.redraw();
        }
    }

    @Override
    public void gotoLocation(Location loc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public WorldWindowGLCanvas getWwd() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void DrawProductOutline(DrawContext dc) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void getColor(int x, int y) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public LLHAreaLayer getVolumeLayer() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
